package com.kangtushizhe.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.kangtushizhe.mapper.UserRoleMapper;
import com.kangtushizhe.emuns.*;
import com.kangtushizhe.service.UserRoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LiuZiMing
 * @since 2024-01-26
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {
    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public List<RoleDTO> getRolesByUserId(Integer userId) {
        MPJLambdaWrapper<UserRole> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper.eq("t.user_id", userId)
                .selectAll(UserRole.class)
                .selectAll(Role.class)
                .leftJoin(Role.class, Role::getId, UserRole::getRoleId);
        List<RoleDTO> rolesList = userRoleMapper.selectJoinList(RoleDTO.class, mpjLambdaWrapper);
        return rolesList;
    }

    @Override
    public List<PermissionDTO> getPermissionsByUserId(Integer userId) {
        MPJLambdaWrapper<UserRole> mpjLambdaWrapper2 = new MPJLambdaWrapper<>();
        mpjLambdaWrapper2.eq("t.user_id", userId)
                .selectAll(UserRole.class)
                .select(RolePermission::getPermissionId)
                .selectAll(Permission.class)
                .leftJoin(RolePermission.class, RolePermission::getRoleId, UserRole::getRoleId)
                .leftJoin(Permission.class, Permission::getId, RolePermission::getPermissionId);
        List<PermissionDTO> permissionList = userRoleMapper.selectJoinList(PermissionDTO.class, mpjLambdaWrapper2);
        return permissionList;
    }
}
